package com.iemes.controller.warehouse;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.AnalyzeformFormMap;
import com.iemes.entity.EnterstorageFormMap;
import com.iemes.entity.LabelMaterialFormMap;
import com.iemes.entity.OrderFormMap;
import com.iemes.entity.OrderperFormMap;
import com.iemes.entity.OvertimeanalyzeFormMap;
import com.iemes.entity.WarehouseStockFormMap;
import com.iemes.mapper.shape.ShapeMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;
import com.iemes.util.MyDateUtils;

@RequestMapping("/enterstorage_order_form/")
@Controller
public class EnterstorageOrder_FormController extends BaseController {

	@Inject
	private ShapeMapper shapeMapper;

	// 查看生产日报表，显示按钮。
	@RequestMapping("enterstorage_order_all")
	public String enterstorage_form(Model model) {
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/warehouse/enterstorage_order_list";
	}
	
	// 生产日报表分页
	@ResponseBody
	@RequestMapping("enterstorage_order_findByPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception {
		OrderFormMap orderFormMap = getFormMap(OrderFormMap.class);
		List<Map<String, Object>> order = shapeMapper.getAll(orderFormMap);
		LuogicPaging(pageNow, pageSize, order);
		return pageView;
	}
	
	// 编辑入库报表
	@RequestMapping("enterstorage_order_form_edit")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		if(Common.isNotEmpty(id)){
			//根据id获得成型产品表
			OrderFormMap order=shapeMapper.getById(id);
			model.addAttribute("order",order);
			//获得相同shapeId的入库报表
			EnterstorageFormMap enter=baseMapper.findbyFrist("orderId", order.get("id").toString(), EnterstorageFormMap.class);
			model.addAttribute("enter",enter);
//				//获取入库报表用料
//				EnterstorageFormMap enterstorageFormMap = getFormMap(EnterstorageFormMap.class);
//				List<Map<String, Object>> process = enterstorageMapper.getEntryByPrstate(enterstorageFormMap);
//				for(Map<String, Object>map:process){
//					//获取今天用料
//					EntryFormMap entryFormMap=entryMapper.getTodaypur();
//					System.out.println("entryFormMap："+entryFormMap.get("name").toString());
//					System.out.println("Materials_Brand："+map.get("Materials_Brand").toString());
//					System.out.println("now_mold_encode："+map.get("now_mold_encode").toString());
//					System.out.println("prmold："+map.get("prmold").toString());
//					if(((entryFormMap.get("name").toString().equals(map.get("Materials_Brand").toString()))
//							&&((map.get("now_mold_encode").toString().equals(map.get("prmold").toString()))))){
////						process= enterstorageMapper.getEntryByPrstate(enterstorageFormMap);
//							model.addAttribute("process",process);
//					}
//				}
		}
		return Common.BACKGROUND_PATH + "/warehouse/enterstorage_order_form_edit";
	}

	// 修改入库报表
	@ResponseBody
	@RequestMapping(value = "enterstorage_order_form_editEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module = "入库报表", methods = "编辑入库报表-修改入库报表") // 凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity(HttpServletRequest request) throws Exception {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		EnterstorageFormMap enterstorageFormMap=getFormMap(EnterstorageFormMap.class);
		// 新增标签子项
		String orderId=request.getParameter("OrderFormMap.id");
		OrderFormMap order=shapeMapper.getById(orderId);
		//设置《预排指令》表的“完成时间”至今天为“已入库天数”
		String date1=order.get("pfinishtime").toString();
		//jsp的storageDay显示换成intervalTime
		enterstorageFormMap.put("storageDay", MyDateUtils.intervalTime(date1,0));
		//设置《预排指令》表的“最后入库日期”
		long millisecond=0;
		//如“是否进烘房产品”字段等于1
		if((int)order.get("oast")==1){
			//推迟24小时后入库
			millisecond=86400000;
		//如“是否进烘房产品”字段等于2
		}else if((int)order.get("oast")==2){
			//推迟3小时后入库
			millisecond=10800000;
		}
		//jsp的finalEnterDate显示换成delayTime
		enterstorageFormMap.put("finalEnterDate", MyDateUtils.delayTime(date1, millisecond));
        //计算每包件数
		enterstorageFormMap.put("orderPack", Math.round((Double.parseDouble(order.get("number").toString()))/(Double.parseDouble(order.get("packNum").toString()))));
		//把生产订单数作为入库数
		enterstorageFormMap.put("numberOfStorage", (Integer.parseInt(order.get("number").toString())));
		enterstorageFormMap.put("orderId", orderId);
		enterstorageFormMap.put("outFinished", 1);
		enterstorageFormMap.put("date", sdf.format(date));
		baseMapper.addEntity(enterstorageFormMap);
		//把预排表改为已入仓状态
		OrderperFormMap orderperFormMap=shapeMapper.getByOrderId(orderId);
		orderperFormMap.put("state", 4);
		baseMapper.editEntity(orderperFormMap);
		OvertimeanalyzeFormMap overtimeanalyzeFormMap=getFormMap(OvertimeanalyzeFormMap.class);
		overtimeanalyzeFormMap.put("enterId", enterstorageFormMap.get("id"));
		overtimeanalyzeFormMap.put("formCategory", 1);
		overtimeanalyzeFormMap.put("dutyPeopleId", 11);
		overtimeanalyzeFormMap.put("abolishPlannerId", 11);
		baseMapper.addEntity(overtimeanalyzeFormMap);
		AnalyzeformFormMap analyzeformFormMap = getFormMap(AnalyzeformFormMap.class);
		analyzeformFormMap.put("enterId", enterstorageFormMap.get("id"));
		baseMapper.addEntity(analyzeformFormMap);
		WarehouseStockFormMap warehouseStockFormMap=getFormMap(WarehouseStockFormMap.class);
		warehouseStockFormMap.put("enterId", enterstorageFormMap.get("id"));
		baseMapper.addEntity(warehouseStockFormMap);
		LabelMaterialFormMap labelMaterialFormMap=getFormMap(LabelMaterialFormMap.class);
		labelMaterialFormMap.put("enterId", enterstorageFormMap.get("id"));
		baseMapper.addEntity(labelMaterialFormMap);
		return "success";
	}
}











